#include "expbycat.h"
#include "ui_expbycat.h"
#include<QtSql/QSqlQuery>
#include<QDesktopWidget>
#include<QLabel>
#include<QMessageBox>
#include<QDebug>
//This allows user to select either categories for categoric view or account for account view
ExpByCat::ExpByCat(int id,QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::ExpByCat)
{
    ui->setupUi(this);
    ui->labelMsg->setVisible(false);
   //  ui->verticalLayout->addSpacing(10);
    page_id=id;
    QString msg;
    QString query;
    if(id==1)
    {
        msg="No Category Found";
        query="select * from category_table";
        this->setWindowTitle(tr("Select Category"));
    }
    else
    {
        msg="No Account Found";
        query="select name from account_table";
        this->setWindowTitle(tr("Select Account"));
    }
    QRect innersize=QApplication::desktop()->availableGeometry(this);
    //ui->listWidget->setGeometry(0,50,(innersize.width()),(innersize.height()));
    ui->scrollArea->setGeometry(0,0,(innersize.width()),(innersize.height()));
    ui->comboBox->setFocus();
    int fontSize=10;
    ui->labelMsg->setFont(QFont("lucida",fontSize,QFont::Bold,true));
    ui->comboBox->setFont(QFont("lucida",(fontSize-2),QFont::Bold,true));
    ui->pushButton->setFont(QFont("lucida",(fontSize-2),QFont::Bold,true));
    QAction* backAction = new QAction( tr("Back"), this );
    backAction->setSoftKeyRole( QAction::NegativeSoftKey );
    connect(backAction, SIGNAL(triggered()), this, SLOT(back()));
    addAction( backAction );

    int c=0;
    if(db.db_connect())
    {
        QSqlQuery q;
        if(q.exec(query))
        {
          while(q.next())
            {
              ui->comboBox->addItem(q.value(0).toString(),Qt::UserRole);
              c++;
          }
        }
        if(c==0)
        {
                 ui->labelMsg->setText(msg);
                 ui->comboBox->setVisible(false);
                 ui->pushButton->setVisible(false);
                 ui->labelMsg->setVisible(true);

        }


        db.db_close();
    }
    else
        QMessageBox::warning(0,tr("Error !"),tr("Error ! Can not open database"));

}

ExpByCat::~ExpByCat()
{
    delete ui;
}
void ExpByCat::back()
{
    (new ExpView)->show();
}


void ExpByCat::on_pushButton_clicked()
{
    (new ExpCatView(page_id,ui->comboBox->currentText()))->show();
}
